#include <iostream>
#include <string>
#include <cstring>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <cstdlib>

using namespace std;

int main()
{
  int sock=socket(AF_INET,SOCK_STREAM,0);
  if(sock<0)
  {
    cerr<<"socket error"<<endl;
    exit(1);
  }

   struct sockaddr_in local;
  memset(&local,0,sizeof(local));
  local.sin_family=AF_INET;
  local.sin_port=htons(8081);
  local.sin_addr.s_addr=INADDR_ANY;
   if(bind(sock,(struct sockaddr*)&local,sizeof(local))<0)
   {
     cerr<<" bind error"<<endl;
     exit(2);
   }
 
   if(listen(sock,5)<0)
   {
     cerr<<"listen error"<<endl;
     exit(3);
   }

   struct sockaddr_in peer;
  for(;;)
   {
    socklen_t len=sizeof(peer);
    int sockfd=accept(sock,(struct sockaddr*)&peer,&len);
    if(sockfd<0)
    {
      cerr<<"accept error"<<endl;
      continue;
    }

    if(fork()==0)
    {
      if(fork()>0)
      {
        exit(0);
      }
      
      close(sock);
      char buff[1024];
      recv(sockfd,buff,sizeof(buff),0);
      cout<<"###################################################"<<endl;
      cout<<buff<<endl;
      cout<<"###################################################"<<endl;
    
      
      
      
      close(sockfd);
      exit(0);
    }
    close(sockfd);
    waitpid(-1,nullptr,0);
    sleep(1);
   }

return 0;
}
